package com.huadao.cart_ccs.modle;

import com.huadao.cart_ccs.base.BaseSerialPortModel;
import com.huadao.cart_ccs.common.Constants;
import com.huadao.cart_ccs.common.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExceptionModle extends BaseSerialPortModel {

    public List getExceptionFileList(){
        File filesDir = new File(Constants.CrashFilePath);
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(FileUtils.CRASH_REPORTER_EXTENSION);
            }
        };
        List fileList=new ArrayList();
        String[] fileArray=filesDir.list(filter);
        if(fileArray!=null) {
            fileList=Arrays.asList(fileArray);
        }
        return fileList;
    }

    public String getExceptionContents(String filePath){
        StringBuilder stringBuilder=new StringBuilder();
        try {
            FileReader fileReader = new FileReader(Constants.CrashFilePath+File.separator+filePath);
            char[] charBuf=new char[1024];
            int len ;
            while((len = fileReader.read(charBuf)) != -1) {
                stringBuilder.append(charBuf,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
